/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "itemlist.h"



namespace tri {
namespace ui {

extItemList::extItemList()
	: tri::ui::extPanel(),
	  mSelected(""),
	  mItemHeight(20),
	  mIconSize(0){
	setStyle("DefaultItemListStyle");
}
	
extItemList::~extItemList(){
	for ( int i = 0;i<mButtons.size() ; i++ ){
		delete mButtons[i];
	}
}


void extItemList::setIconSize(float s){
	mIconSize = s;
}

void extItemList::setItemHeight(float h){
	mItemHeight = h;
}

void extItemList::setStyle(std::string style){
	tri::ui::extPanel::setStyle(style);
	
	for ( int i = 0;i<mButtons.size() ; i++ ){
		mButtons[i]->setStyle(style+std::string(".ListItem"));
	}
}

void extItemList:: pack(){	
	if(mItems.size()>mButtons.size()){
		int count = mItems.size()-mButtons.size();
		for (int i = 0;i<count;i++){
			addButton();
		}
	}
	
	for ( int i = 0;i<mButtons.size() ; i++ ){
		mButtons[i]->setVisible(false);
		mIcons[i]->setVisible(false);
	}
	
	tri::TVector  p_size =  getSize();
	p_size.y = getInsets().getTop()+getInsets().getBottom()+mItems.size()*mItemHeight;
	setSize(p_size);
	float iconWidth = mItemHeight<mIconSize?mItemHeight:mIconSize;
	float pos_c = p_size.y-getInsets().getTop()-mItemHeight;
	for ( unsigned int i = 0;i<mItems.size() ; i++ ){
		float button_width = p_size.x-getInsets().getLeft()-getInsets().getRight()-iconWidth;
		mButtons[i]->setText(mItems[i].c_str());
		mButtons[i]->setVisible(true);
		mButtons[i]->setSize(button_width,mItemHeight,0);
		mButtons[i]->setPosition(iconWidth+getInsets().getLeft(),pos_c,0);
		
		mIcons[i]->setImage(mItemIcons[i]);
		mIcons[i]->setVisible(true);
		mIcons[i]->setSize(iconWidth,iconWidth,iconWidth);
		mIcons[i]->setPosition(getInsets().getLeft(),pos_c,0);
		
		pos_c-=mItemHeight;
	}	
}

void extItemList::update(){
	pack();
	tri::ui::extPanel::update();
}

void extItemList::addItem(std::string item,std::string icon){
	if(!hasItem(item)){
		mItems.push_back(item);
		mItemIcons.push_back(icon);
	}
}

void extItemList::removeItem(std::string item){
	if(hasItem(item)){
		mItems.erase(mItems.begin()+index(item));
		mItemIcons.erase(mItemIcons.begin()+index(item));
		if (mSelected == item){
			mSelected = "";
		}
	}
}

int extItemList::index(std::string item){
	for ( unsigned int i =0 ; i<mItems.size();i++ ){
		if(mItems[i]==item){
			return i;
		}
	}
	return -1;
}

int extItemList::size(){
	return mItems.size();
}

void extItemList::clear(){
	mItems.clear();
	mItemIcons.clear();
}

bool extItemList::hasItem(std::string item){
	for ( unsigned int i =0 ; i<mItems.size();i++ ){
		if(mItems[i]==item){
			return true;
		}
	}
	return false;
}

void extItemList::addButton(){
	tri::ui::extButton* button = new tri::ui::extButton();
	button->setStyle(getStyle()+std::string(".ListItem"));
	button->connect<extItemList>(tri::BUTTON_RELEASED,this,&extItemList::onButtonClicked);
	mButtons.push_back(button);
	button->setTarget("Target Item : " + tri::utils::int2String(mButtons.size()));
	addComponent(button);
	
	
	tri::ui::extPanel* icon = new tri::ui::extPanel();
	icon->setStyle("");
	mIcons.push_back(icon);
	addComponent(icon);
}

void extItemList::onButtonClicked(std::string target){
	for ( unsigned int i =0 ; i<mButtons.size();i++ ){
		if(mButtons[i]->getTarget()==target){
			if (mSelected != mButtons[i]->getText()){
				mSelected = mButtons[i]->getText();
				emit(tri::VALUE_CHANGED);
			}
		}
	}
}

std::string extItemList::getSelectedItem(){
	return mSelected;
}

}
}
